package com.unionftech.content_user.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.SwitchCompat
import com.unionftech.common.CommonConstants
import com.unionftech.common.R.string
import com.unionftech.common.base.BaseFragment
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.SpUtils
import com.unionftech.common.utils.StatusBarUtil
import com.unionftech.common.utils.Utils
import com.unionftech.content_user.R

class UFSkinSettingFragment : BaseFragment() {
    private val mFlSkinContent: LinearLayout by lazy { mRootView.findViewById(R.id.fl_skin_content) }
    private val mIvSkinLogo: ImageView by lazy { mRootView.findViewById(R.id.iv_skin_logo) }
    private val mTvChangeSkinTitle: TextView by lazy { mRootView.findViewById(R.id.tv_change_skin_title) }
    private val mTvChangeSkinSub: TextView by lazy { mRootView.findViewById(R.id.tv_change_skin_sub) }
    private val mLlLightThemes: LinearLayout by lazy { mRootView.findViewById(R.id.ll_light_themes) }
    private val mIvLightThemes: ImageView by lazy { mRootView.findViewById(R.id.iv_light_themes) }
    private val mTvLight: TextView by lazy { mRootView.findViewById(R.id.tv_light) }
    private val mIvLightThemesRadio: ImageView by lazy { mRootView.findViewById(R.id.iv_light_themes_radio) }
    private val mLlNightThemes: LinearLayout by lazy { mRootView.findViewById(R.id.ll_night_themes) }
    private val mIvNightThemes: ImageView by lazy { mRootView.findViewById(R.id.iv_night_themes) }
    private val mTvDark: TextView by lazy { mRootView.findViewById(R.id.tv_dark) }
    private val mIvNightThemesRadio: ImageView by lazy { mRootView.findViewById(R.id.iv_night_themes_radio) }
    private val mTvFollowSystem: TextView by lazy { mRootView.findViewById(R.id.tv_follow_system) }
    private val mBtnFollowSystem: SwitchCompat by lazy { mRootView.findViewById(R.id.btn_follow_system) }
    private val mBtnConfirm: Button by lazy { mRootView.findViewById(R.id.btn_confirm) }
    private var mIsUserControl = true

    private var mCallback: (() -> Unit)? = null

    companion object {
        @JvmStatic
        fun newInstance() = UFSkinSettingFragment().apply {
            arguments = Bundle().apply {

            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun inflaterContentView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_u_f_skin_setting, container, false)
    }

    override fun initView(view: View) {
        super.initView(view)
        setThemes()
    }

    override fun initLanguageText() {
        super.initLanguageText()
        mTvChangeSkinTitle.text =
            DynamicResourceManager.getString(mContext, R.string.choose_your_theme)
        mTvChangeSkinSub.text = DynamicResourceManager.getString(mContext, R.string.change_theme)
        mTvLight.text = DynamicResourceManager.getString(mContext, R.string.light_theme)
        mTvDark.text = DynamicResourceManager.getString(mContext, R.string.dark_theme)
        mTvFollowSystem.text = DynamicResourceManager.getString(mContext, R.string.follow_system)
        mBtnConfirm.text = DynamicResourceManager.getString(mContext, string.confirm)
    }

    override fun setListener() {
        super.setListener()
        mLlLightThemes.setOnClickListener(this)
        mLlNightThemes.setOnClickListener(this)
        mBtnConfirm.setOnClickListener(this)
        mBtnFollowSystem.setOnCheckedChangeListener { _, isChecked ->
            if (mIsUserControl) {
                val isDark = Utils.isDarkModeEnable(mContext)
                Utils.switchThemes(isDark)
                setThemesSelectEnable(darkModeEnable = isDark)
            }
        }
    }

    private fun setThemesSelectEnable(darkModeEnable: Boolean, isFollowChecked: Boolean? = null) {
        mIvNightThemesRadio.isSelected = darkModeEnable
        mIvNightThemes.isSelected = darkModeEnable
        mIvLightThemesRadio.isSelected = !darkModeEnable
        mIvLightThemes.isSelected = !darkModeEnable
        isFollowChecked?.also {
            if (!mIsUserControl) {
                if (mBtnFollowSystem.isChecked != it) {
                    mBtnFollowSystem.isChecked = it
                }
                mIsUserControl = true
            }
        }
    }

    override fun setView() {
        super.setView()
        hideTitleBar()
        StatusBarUtil.setPaddingSmart(mContext, mFlSkinContent)
    }

    private fun setThemes() {
        val isFollowSystem =
            SpUtils.getInstance().getBoolean(CommonConstants.Key.KEY_FOLLOW_SYSTEM, false)
        var isDark = SpUtils.getInstance().getBoolean(CommonConstants.Key.KEY_IS_DARK_MODE, false)
        //获取当前系统的模式
        if (isFollowSystem) {
            isDark = Utils.isDarkModeEnable(mContext)
        }
        mIsUserControl = false
        Utils.switchThemes(isDark)
        setThemesSelectEnable(darkModeEnable = isDark, isFollowChecked = isFollowSystem)
    }

    override fun doClick(v: View?) {
        super.doClick(v)
        when (v?.id) {
            R.id.ll_light_themes -> {
                if (!mIvLightThemes.isSelected) {
                    Utils.switchThemes(false)
                    mIsUserControl = false
                    setThemesSelectEnable(darkModeEnable = false, isFollowChecked = false)
                }
            }

            R.id.ll_night_themes -> {
                if (!mIvNightThemes.isSelected) {
                    Utils.switchThemes(true)
                    mIsUserControl = false
                    setThemesSelectEnable(darkModeEnable = true, isFollowChecked = false)
                }
            }

            R.id.btn_confirm -> {
                confirmAppThemes()
            }
        }
    }

    fun setCallBack(callBack: () -> Unit) {
        this.mCallback = callBack
    }

    private fun confirmAppThemes() {
        SpUtils.getInstance().put(CommonConstants.Key.KEY_FOLLOW_SYSTEM, mBtnFollowSystem.isChecked)
        SpUtils.getInstance()
            .put(CommonConstants.Key.KEY_IS_DARK_MODE, mIvNightThemesRadio.isSelected)
        mCallback?.invoke()
    }

    override fun onDestroy() {
        mCallback = null
        super.onDestroy()
    }
}